package org.finesys.gateway.handler;

import org.finesys.common.core.module.R;
import org.finesys.gateway.util.WebFluxUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.anji.captcha.model.common.CaptchaTypeEnum;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;

import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;

/**
 *
 */
@Component
@RequiredArgsConstructor
public class AjImageCodeCreateHandler implements HandlerFunction<ServerResponse> {
    private final CaptchaService captchaService;

    @Override
    public Mono<ServerResponse> handle(@NonNull ServerRequest request) {
        CaptchaVO vo = new CaptchaVO();
        vo.setCaptchaType(CaptchaTypeEnum.BLOCKPUZZLE.getCodeValue());
        vo.setBrowserInfo(WebFluxUtil.getClientIP(request));
        ResponseModel responseModel = captchaService.get(vo);
        return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(R.success(responseModel)));
    }
}
